package com.lovo.frame;

import com.lovo.pojo.UserDO;
import com.lovo.service.UserService;
import com.lovo.service.impl.UserServiceImpl;
import com.lovo.util.Util;
import com.lovo.util.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 转账界面
 */
public class TransferPanel extends JPanel implements ActionListener {
    private ATM frame;
    private JLabel otherAccountLabel = new JLabel("请输入对方账号");
    private JLabel blanceLabel = new JLabel("请输入转账金额");
    private JTextField otherAccountTextField = new JTextField();
    private JTextField blanceTextField = new JTextField();
    private JButton sureBtn = new JButton(Util.getIcon("/images/buttonSure.jpg"));
    private JButton backBtn = new JButton(Util.getIcon("/images/buttonOff.jpg"));
    private Font font = new Font("宋体",Font.BOLD,25);
    private UserService userService = new UserServiceImpl();
    public TransferPanel(ATM frame){
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        otherAccountLabel.setBounds(300,200,200,50);
        otherAccountLabel.setFont(font);
        this.add(otherAccountLabel);

        otherAccountTextField.setBounds(500,200,200,50);
        otherAccountTextField.setFont(font);
        this.add(otherAccountTextField);

        blanceLabel.setBounds(300,250,200,50);
        blanceLabel.setFont(font);
        this.add(blanceLabel);

        blanceTextField.setBounds(500,250,200,50);
        blanceTextField.setFont(font);
        this.add(blanceTextField);

        sureBtn.setBounds(350,350,140,50);
        sureBtn.addActionListener(this);
        this.add(sureBtn);

        backBtn.setBounds(520,350,140,50);
        backBtn.addActionListener(this);
        this.add(backBtn);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == sureBtn){
            // 获取输入的对方账号
            String otherAccount = otherAccountTextField.getText();
            double blance = 0;
            try {
                // 获取输入的转账金额
                blance = Double.parseDouble(blanceTextField.getText());
            }catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(this,"转账金额必须为数字，并不能为空");
                return;
            }

            // 当期登录的账户
//            DBUtil db = new DBUtil();
//            UserDO loginUser = db.getById(frame.getLoginUser().getId());
//            UserDO otherUser = db.getByUsername(otherAccount);
////            ArrayList<User> users = frame.getUsers();
////
////            boolean isExist = false;
////            for(int i = 0;i < users.size();i++){
////                if(users.get(i).getAccount().equals(otherAccount)){
////                    isExist = true;
////                    otherUser = users.get(i);
////                    break;
////                }
////            }
//            // 判断账号是否存在
//            if(otherUser == null){
//                JOptionPane.showMessageDialog(this,"查无此人");
//                return;
//            }
//            // 判断是否转给自己
//            if(loginUser.getAccount().equals(otherAccount)){
//                JOptionPane.showMessageDialog(this,"不能转给自己");
//                return;
//            }
//            // 判断余额是否足够
//            if(blance > loginUser.getBlance()){
//                JOptionPane.showMessageDialog(this,"余额不足");
//                return;
//            }
//            // 当期账户扣钱
//            loginUser.setBlance(loginUser.getBlance() - blance);
//            // 对方账户加钱，原子
//            otherUser.setBlance(otherUser.getBlance() + blance);

            try{
                userService.transfer(frame.getLoginUser().getId(),otherAccount,blance);
                JOptionPane.showMessageDialog(this,"转账成功");
            }catch(Exception e1){
                JOptionPane.showMessageDialog(this,e1.getMessage());
            }

            frame.switchPanel("menu");


        }else if(e.getSource() == backBtn){
            frame.switchPanel("menu");

        }
    }
}
